//
//  LoginView.m
//  YiXiProject
//
//  Created by iMac on 2019/7/16.
//  Copyright © 2019 iMac. All rights reserved.
//

#import "LoginView.h"

@implementation LoginView

-(UIButton *)disMissBtn
{
    if(!_disMissBtn)
    {
        _disMissBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [_disMissBtn setTitle:@"X" forState:UIControlStateNormal];
        [_disMissBtn setTitleColor:WhiteColor forState:UIControlStateNormal];
        _disMissBtn.titleLabel.font = FontCustom(18, 1);
        _disMissBtn.tag = 102;
        [_disMissBtn addTarget:self action:@selector(buttonLogin:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _disMissBtn;
}

-(UITextField *)emailText
{
    if(!_emailText)
    {
        _emailText = [[UITextField alloc]init];
        _emailText.backgroundColor = WhiteColor;
        _emailText.font = Font(13);
        _emailText.layer.cornerRadius = 20;
        _emailText.layer.masksToBounds = YES;
        _emailText.placeholder = @"Enter email";
        _emailText.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 40)];
        _emailText.leftViewMode = UITextFieldViewModeAlways;  // 总是显示
        _emailText.clearButtonMode = UITextFieldViewModeWhileEditing;
        
        _emailText.tag = 100;
        _emailText.delegate = self;
    }
    return _emailText;
}

-(UITextField *)password
{
    if(!_password)
    {
        _password = [[UITextField alloc]init];
        _password.backgroundColor = WhiteColor;
        _password.font = Font(13);
        _password.layer.cornerRadius = 20;
        _password.layer.masksToBounds = YES;
        _password.placeholder = @"Enter password";
        _password.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 40)];
        _password.leftViewMode = UITextFieldViewModeAlways;  // 总是显示
        _password.clearButtonMode = UITextFieldViewModeWhileEditing;
        _password.secureTextEntry = YES;
        _password.tag = 101;
        _password.delegate = self;
    }
    return _password;
}

-(UIButton *)loginBtn
{
    if(!_loginBtn)
    {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [_loginBtn setTitle:@"Login" forState:UIControlStateNormal];
        [_loginBtn setTitleColor:WhiteColor forState:UIControlStateNormal];
        _loginBtn.titleLabel.font = Font(15);
        [_loginBtn setBackgroundColor:[UIColor colorWithRed:0.8 green:0 blue:0.3 alpha:1]];
        _loginBtn.layer.cornerRadius = 20;
        _loginBtn.layer.masksToBounds = YES;
        _loginBtn.tag = 100;
        [_loginBtn addTarget:self action:@selector(buttonLogin:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}

-(UIButton *)goBackBtn
{
    if(!_goBackBtn)
    {
        _goBackBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [_goBackBtn setTitle:@"Go register" forState:UIControlStateNormal];
        [_goBackBtn setTitleColor:WhiteColor forState:UIControlStateNormal];
        _goBackBtn.titleLabel.font = Font(15);
        _goBackBtn.tag = 101;
        [_goBackBtn addTarget:self action:@selector(buttonLogin:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _goBackBtn;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        [self addSubview:self.disMissBtn];
        [self addSubview:self.emailText];
        [self addSubview:self.password];
        [self addSubview:self.loginBtn];
        [self addSubview:self.goBackBtn];
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    float heightTwo;
    if(SCREEN_HEIGHT > 736)
    {
        heightTwo = 88;
    }
    else
    {
        heightTwo = 64;
    }
    
    _disMissBtn.sd_layout.leftSpaceToView(self, 30).topSpaceToView(self, heightTwo-30).widthIs(25).heightIs(25);
    _emailText.sd_layout.leftSpaceToView(self, 50).topSpaceToView(self, self.bounds.size.height/2-80).rightSpaceToView(self, 50).heightIs(50);
    _password.sd_layout.leftSpaceToView(self, 50).topSpaceToView(_emailText, 15).rightSpaceToView(self, 50).heightIs(50);
    _loginBtn.sd_layout.leftSpaceToView(self, 50).topSpaceToView(_password, 20).rightSpaceToView(self, 50).heightIs(50);
    _goBackBtn.sd_layout.leftSpaceToView(self, 50).bottomSpaceToView(self, 80).rightSpaceToView(self, 50).heightIs(30);
}

// 登陆按钮方法
-(void)buttonLogin:(UIButton *)butt
{
    if ([Regular isVerificationEmail:self.emailText.text] && self.password.text.length >= 6 && self.password.text.length <=20 )
    {
        _isSuccess = 1;
    }
    else
    {
        _isSuccess = 0;
    }
    
    if(butt.tag == 100)
    {
        if(!_isSuccess)
        {
            [SVProgressHUD showErrorWithStatus:@"登陆信息填写不正确"];
            [SVProgressHUD dismissWithDelay:SVPROGRESSHUDTIME];
        }
        else
        {
            if([_delegate respondsToSelector:@selector(clickLoginButton:)])
            {
                [_delegate clickLoginButton:butt.tag];
            }
        }
    }
    else
    {
        if([_delegate respondsToSelector:@selector(clickLoginButton:)])
        {
            [_delegate clickLoginButton:butt.tag];
        }
    }
}

//开始编辑调用
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    textField.textColor = BlackColor;
}

// 编辑完调用
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    switch (textField.tag)
    {
        case 100:
            
            if(![Regular isVerificationEmail:textField.text])
            {
                textField.textColor = RedColor;
            }
            
            break;
        
        case 101:
            if(textField.text.length < 6 || textField.text.length > 20)
            {
                textField.textColor = RedColor;
            }
            
            break;
            
        default:
            break;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
